package com.johnny.xia.filetest;

import com.johnny.xia.util.base.ValidatorUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class ApolloFileCheck {

    public static void main(String[] args) {

//        String hubProd = "/Users/johnny/Desktop/bff-apollo-prod.txt";
//        String hubUat = "/Users/johnny/Desktop/bff-apollo-uat.txt";
        String hubProd = "/Users/johnny/Documents/宝尊/RIC/tmall/TmallHubadapter/hub-apollo-prod-v3.txt";
        String hubUat = "/Users/johnny/Documents/宝尊/RIC/tmall/TmallHubadapter/hub-apollo-uat-v2.txt";

        LinkedHashMap<String,String> prodMap = readFileByLines(hubProd);
        LinkedHashMap<String,String> uatMap = readFileByLines(hubUat);

        for(Map.Entry<String,String>  entry : uatMap.entrySet()){
            if(prodMap.get(entry.getKey()) == null){
                System.err.println(entry.getKey());
            }
        }

        System.out.printf("proMap:"+prodMap.size()+",uatMap:"+uatMap.size());

    }

    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    public static LinkedHashMap<String,String> readFileByLines(String fileName){
        LinkedHashMap<String,String> map = new LinkedHashMap<>();

        File file = new File(fileName);
        BufferedReader reader = null;
        try{
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null){
                if(ValidatorUtil.isNotNullOrEmpty(tempString)){
                    String array[] = tempString.split("=");
                    map.put(array[0].trim(),"");
                }

//                String array[] = tempString.split("=");
//                if( !array[0].equalsIgnoreCase("") && array.length >= 1 && array.length <= 2){
//                    map.put(array[0].trim(),"");
//                }else{
//                    System.err.println("错误："+tempString);
//                }
            }
            reader.close();
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            if (reader != null){
                try{
                    reader.close();
                }catch (IOException e1){}
            }
        }
        return map;
    }
}
